#include "Goal_ConstructBuilding.h"
#include "Goal_TakePathToRange.h"
#include "PathPlanner.h"
#include "Character.h"
#include "Building.h"
#include "Game Factory.h"

Goal_ConstructBuilding::Goal_ConstructBuilding(Character* owner, GameBuildingID type, Vector2D position) : Goal_Composite(owner, goal_construct_building)
{
	m_pBuilding = Factory->CreateBuilding(type, position);
	World->AddEntity(m_pBuilding); 
}

void Goal_ConstructBuilding::Activate()
{
	m_iStatus = active;

	// The range the character must enter before beginning to construct the building
	// The bare minimum should be the sum of the bounding radius,

	double Range = m_pBuilding->BRadius() + m_pOwner->BRadius() + 20;
	AddSubgoal(new Goal_TakePathToRange(m_pOwner, m_pBuilding->Pos(), Range, OVER_BUILDING));
}

int Goal_ConstructBuilding::Process()
{
	ActivateIfInactive();

	ProcessSubgoals();

	// The subgoal list will be empty once this character
	// reaches the building destination
	if(m_SubGoals.empty())
	{
		m_pBuilding->Repair();
		if(m_pBuilding->CurrHealth() == m_pBuilding->MaxHealth())
			m_iStatus = completed;
	}
	return 0;
}

void Goal_ConstructBuilding::Terminate()
{
}

// Handle Message
// If a message is received forcing the character to do something else, then he should stop
// building

bool Goal_ConstructBuilding::HandleMessage(const Message& msg)
 {
	 // Currently, all messages froce the character to do something different.
	 m_iStatus = failed;
	 return true;
 }
